﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Core.IService;
using Infragistics.Win.UltraWinGrid;
using FX.Core;
using CRM.Admin;
using BHXH.Core.Domain;

namespace BHXH.Frm.FrmUserControl
{
    public partial class BBTT_BHYT : UserControl
    {
        private readonly Ihs_BBTT_dtService _hs_bbttdtSrv;
        int STT = 0;
        IList<string> ID_BBTTDT = new List<string>();
        public BBTT_BHYT()
        {
            InitializeComponent();
            _hs_bbttdtSrv = IoC.Resolve<Ihs_BBTT_dtService>();

            LoadGridview_HS_BBTTDT();
        }
        public DataTable GetAll_HS_BBTTDT_Tab()
        {
            IList<hs_BBTT_dt> a = _hs_bbttdtSrv.GetAll_hs_bbttdt();
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("STT", typeof(string));
            table.Columns.Add("Họ và tên", typeof(string));
            table.Columns.Add("SỐ SỐ BHXH", typeof(string));
            table.Columns.Add("Số THẺ BHYT", typeof(string));
            table.Columns.Add("Ngày TRẢ", typeof(string));
            table.Columns.Add("GIÁ TRỊ TRẢ TỪ NGÀY", typeof(string));
            table.Columns.Add("GHI CHÚ", typeof(string));
            if (a.Count > 0)
            {
                foreach (var item in a)
                {
                    table.Rows.Add(item.ID, item.CT01, item.CT02, item.CT03, item.CT04, item.CT05, item.CT06, item.CT07);
                    STT = int.Parse(item.CT01);
                }
            }
            else
            {
                STT += 1;
                table.Rows.Add(null,STT, null, null, null, null, null, null);

            }

            return table;
        }
        public void LoadGridview_HS_BBTTDT()
        {

            uG_BBTTBHYT.DataSource = GetAll_HS_BBTTDT_Tab();
            this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
        }
        public void Ghi_BBTTDT()
        {
            string ID = "";
            bool update = false;
            bool add = false;
            hs_BBTT_dt hs;
            UltraGridBand band = this.uG_BBTTBHYT.DisplayLayout.Bands[0];
            foreach (UltraGridRow row in band.GetRowEnumerator(GridRowType.DataRow))
            {
                hs = new hs_BBTT_dt();
                UltraGridColumn column = this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[0];
                ID = row.Cells[column].Value.ToString();
                hs.CT01 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[1]].Value.ToString();
                hs.CT02 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[2]].Value.ToString();
                hs.CT03 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[3]].Value.ToString();
                hs.CT04 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[4]].Value.ToString();
                hs.CT05 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[5]].Value.ToString();
                hs.CT06 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[6]].Value.ToString();
                hs.CT07 = row.Cells[this.uG_BBTTBHYT.DisplayLayout.Bands[0].Columns[7]].Value.ToString();
                if (!ID.Equals(""))
                {
                    hs.ID = int.Parse(ID);
                    if (_hs_bbttdtSrv.Update_hs_bbttdt(hs) == true)
                    {
                        update = true;
                    }        
                }
                else
                {
                    if (_hs_bbttdtSrv.Add_hs_bbttdt(hs) == true)
                    {
                        add = true;
                    }
                }
            }
        }
        public void Ghi()
        {
            Ghi_BBTTDT();
            LoadGridview_HS_BBTTDT();
        }
        public void Insert()
        {
            UltraGridRow row = this.uG_BBTTBHYT.DisplayLayout.Bands[0].AddNew();
            STT += 1;
            row.Cells[1].Value = STT;
            row.Cells[2].Value = "";
            row.Cells[3].Value = "";
            row.Cells[4].Value = "";
            row.Cells[5].Value = "";
            row.Cells[6].Value = "";
            row.Cells[7].Value = "";
        }
        public void XoaDong()
        {
            if (this.uG_BBTTBHYT.Selected.Rows.Count > 0)
            {
                ID_BBTTDT.Add(this.uG_BBTTBHYT.ActiveRow.Cells["ID"].Value.ToString());
                this.uG_BBTTBHYT.DeleteSelectedRows();
                STT -= 1;
            }
            else
            {
                MessageBox.Show("Chọn dòng để xóa.");
            }
        }
        public void XoaAll()
        {
            bool Xoa_hs_bbttdt = false;
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                foreach (var item in ID_BBTTDT)
                {
                    if (_hs_bbttdtSrv.Delete_hs_bbttdt(item) > 0)
                    {
                        Xoa_hs_bbttdt = true;
                    }
                }
                if (Xoa_hs_bbttdt == true)
                {
                    MessageBox.Show("Xóa thông tin thành công...");
                }
            }
        }
        private void uG_BBTTBHYT_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    Ghi();
                    break;
                case Keys.Insert:
                    Insert();
                    break;
                case Keys.F11:
                    XoaDong();
                    break;
                case Keys.F3:
                    XoaAll();
                    break;
            }
        }

        private void uG_BBTTBHYT_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;

            // Display our own custom message box.
            System.Windows.Forms.DialogResult result =
                System.Windows.Forms.MessageBox.Show(
                "Bạn có chắc chắn xóa không?",
                "Thông báo",
                System.Windows.Forms.MessageBoxButtons.YesNo,
                System.Windows.Forms.MessageBoxIcon.Question);

            // If the user clicked No on the message box, cancel the deletion of rows.
            if (System.Windows.Forms.DialogResult.No == result)
                e.Cancel = true;
        }
        
    }
}
